//
//  InitConfig.m
//  insurance
//
//  Created by li on 2018/6/19.
//  Copyright © 2018年 li. All rights reserved.
//

#import "InitConfig.h"
#import <IQKeyboardManager.h>
#import "CheckVersionViewModel.h"
#import "MainWebController.h"
//#import <WXApi.h>
//#import <OSHService.h>
#import "IDCardCameraViewController.h"
#import "BaseNavigationController.h"
#import <Bugly/Bugly.h>

@interface InitConfig ()

@property (nonatomic,strong) UIViewController *mainController;

@end

@implementation InitConfig

- (void)initConfigWithWindow:(UIWindow *)window application:(UIApplication *)application options:(NSDictionary *)launchOptions {
    
    [self bugly];  //bugly统计
//    [CheckVersionViewModel getWebVersion];
    
//    [self checkIfLoginWithWindow:window];
    [self checkNewVersionWithNeedTips:NO]; //版本更新检测
    //todo: 测试
    [HSTool clearWebCache];
    
    [HSTool setStatusBarBackgroundColor:kHexColor(0x368bf4)];
    window.rootViewController = self.mainController;
    [window makeKeyAndVisible];
    
    [IQKeyboardManager sharedManager].enable = YES;
    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
}


/**
 检查新版本
 
 */
- (void)checkNewVersionWithNeedTips:(BOOL)isNeedTips{
    
    [CheckVersionViewModel getVersionInfoComplete:^(BOOL isSuccess, NSString *errorMeesage, VersionModel *model) {
        
        if (!isSuccess) {
//            NSString * error = [NSString stringWithFormat:@"获取版本信息失败:%@",errorMeesage?:@""];
//            if (isNeedTips) {
//                kShowAlertMsg(error);
//            }
//
//            HSLog(@"%@",error);
            return;
        }else{
            
            if (model.versionCode.intValue > kAppBundleVersion) {
                
                if ([model.force_update integerValue] == 1) {
                    NSString *title = [NSString stringWithFormat:@"检测到最新版本,请更新(%d/%@)",kAppBundleVersion,model.message];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [UIAlertView bk_showAlertViewWithTitle:title message:[NSString stringWithFormat:@"更新内容：%@", model.message ?:@""] cancelButtonTitle:@"确定" otherButtonTitles:nil handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            [self downloadNewAppWithUrl:model.download];
                        }];
                    });
                    
                }else if ([model.force_update integerValue] == 0) {
                    NSString *tips = [NSString stringWithFormat:@"有新版本是否马上升级(%d/%@)",kAppBundleVersion,model.versionCode];
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [UIAlertView bk_showAlertViewWithTitle:tips message:[NSString stringWithFormat:@"更新内容：%@", model.message ?:@""] cancelButtonTitle:@"取消" otherButtonTitles:@[@"确定"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
                            if (buttonIndex == 1) {
                                [self downloadNewAppWithUrl:model.download];
                            }
                        }];
                    });
                }
            }else{
                if (isNeedTips) {
                    kShowAlertMsg(@"亲,已经是最新版了！");
                }
            }
        }
    }];
}

- (void)bugly{
    [Bugly startWithAppId:[AppConfig buglyAppKey]];
}

- (UIViewController *)mainController{
   
    MainWebController *con = [[MainWebController alloc] init];
    BaseNavigationController *nav = [[BaseNavigationController alloc] initWithRootViewController:con];
    return nav;
//    return con;
}


/**
 下载新版本
 
 @param url 下载地址
 */
- (void)downloadNewAppWithUrl:(NSURL *)url{
    [[UIApplication sharedApplication] openURL:url];
    [self exitCurrentApp];
}

// 强行退出当前App
- (void)exitCurrentApp {
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:kIsHasLogin];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [UIView animateWithDuration:0.5f animations:^{
        window.alpha = 0;
        window.frame = CGRectMake(0, window.bounds.size.width, 0, 0);
    } completion:^(BOOL finished) {
        exit(0);
    }];
}


@end
